// pages/test/test.js
const db = wx.cloud.database();
const _ = db.command
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
      room_name: '',
      room_password: '',
      room_join_click: false,
      room_watch_click: false,
      is_ok: '',
      start_time: 0,
      choice:0,
      is_submit:false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },
    input_name(e) {
      let temp = e.detail.value
      this.setData({
        room_name: temp,
        is_submit:false
      })
    },
   input_password(e) {
      this.setData({
        room_password: e.detail.value,
        is_submit:false
      })
    },
  join_room(e) {
    let that = this
    let room_name = this.data.room_name
    let room_password = this.data.room_password
    let flag = 0
    let room = {}
    let rooms = []
    let choice = this.data.choice
    this.setData({is_submit:true})
    if (choice == 0) {
      this.setData({
        is_ok: e.currentTarget.dataset.target,
        show_message: '请选择角色',
        is_submit:false
      })
    } else
      wx.cloud.callFunction({
        name: 'get_datas',  // 云函数名称
        data: {  // 传给云函数的参数
          name: 'room',
          filter: {}  //筛选条件，默认为空（即查询所有记录），格式 {key:'values'} 
        },
        complete: res => {
          rooms = res.result.data
          console.log("rooms:", rooms, that.data.room_name);
          for (let i = 0; i < rooms.length; i++) {
            if (rooms[i].room_name === room_name) {
              if (choice == 1) {
                if ((rooms[i].room_password == room_password || this.data.room_watch_click)) {
                  flag = 1
                  room = rooms[i]
                  break
                } else if (rooms[i].room_password != room_password) {
                  flag = 2
                  break
                }
              } else if (choice == 2) {
                flag = 1
                room = rooms[i]
                break
              }
            }
          }
          if (flag == 1) {
            app.globalData.room = room
            app.globalData.role_choice = choice
            console.log("room_message",room);
            if (room.is_start&& choice==1) {
              this.setData({
                is_ok: 'Modal',
                show_message: '房间已经开始，无法再参与游戏',
                is_submit:false 
              })
            } else {
              if (choice == 1)
                wx.navigateTo({
                  url: '/pages/room_center/room_center',
                });
              else
                wx.navigateTo({
                  url: '/pages/viewer/viewer',
                });
            }
          } else if (flag == 2) {
            this.setData({
              is_ok: e.currentTarget.dataset.target,
              show_message: '密码错误请重新输入',
              is_submit:false 
            })
          } else if (flag == 0) {
            this.setData({
              is_ok: e.currentTarget.dataset.target,
              show_message: '查询不到房间号，请重新输入',
              is_submit:false 
            })
          }
        }
      })
    console.log(e);
    
  },
  enter_room_join(e) {
    console.log(e);
    let temp = parseInt(e.target.dataset.choice)
    this.setData({
      choice:temp,
      room_watch_click: false,
      room_join_click:true
    })
  },
  enter_room_watch(e) {
    console.log(e);
    let temp = parseInt(e.target.dataset.choice)
    this.setData({
      choice:temp,
      room_watch_click: true,
      room_join_click:false
    })
  },
  hideModal(e) {
  this.setData({
    is_ok:null
  })
  }
})